Comparison Operators

In javascript, the assignment operators are used to assign values to the variables.

Operator Description
== Checks equality of 2 values
=== Checks equality of value and type
!= Checks whether 2 values are not equal
!== Checks whether 2 values and their type are not equal
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
? Ternary operator

Equality Operator (==)

This operator converts the operands if their types are different and then performs comparison on the values of the operands.

Example

console.log(1 == 1); // true
console.log("3" == 3); // true
console.log(3+"5" == 35) // true
console.log(0 == false); // true
console.log(0 == null); // false
console.log(0 == undefined); // false
console.log(null == undefined); // true

Strict Equality Operator (===)

This operator checks whether the value of the variable and the variable type are equals.

Example

console.log(1 === 1); // true
console.log("3" === 3); // false
console.log(3+"5" === 35) // false
console.log(0 === false); // false
console.log(0 === null); // false
console.log(0 === undefined); // false
console.log(null === undefined); // false

Inequality Operator (!=)

This operator, converts the operands type if they are of different type and checks whether their values are not equal. If they are not equal, then it will return true.

Example

console.log(1 !=  2) ;    // true
console.log(2 != "2");    // false
console.log(3 != '3' );   // false
console.log(1 != true);   // false
console.log(0 !=  false);  // false

Strict Inequality Operators(!==)

This operator is used to check whether their value/type of the 2 variables are not equal. If they are not equal then it will return true.

Example

let x=5;
console.log(x!=='5'); // true

Greater than operator(>)

This operator returns true, if the left operand value is greater than the right operand value.

Example

console.log(7>5); // true

Greater than or equal to operator(>=)

This operator returns true, if the left operand value is greater than or equal to the right operand value.

Example

console.log(7>=5); // true
console.log(5>=5); // true

Less than operator(<)

This operator returns true, if the left operand value is less than the right operand value.

Example

console.log(7<5); // false
console.log(4<5); // true

Less than or equal to operator(<=)

This operator returns true, if the left operand value is less than or equal to the right operand value.

Example

console.log(7<=5); // false
console.log(5<=5); // true

Most Read